package com.haier.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haier.constant.HpConstant;
import com.haier.entity.HpArea;
import com.haier.entity.HpAreaGoCount;
import com.haier.entity.HpAreaGoTrack;
import com.haier.entity.HpConstantEntity;
import com.haier.entity.HpGarden;
import com.haier.mapper.HpAreaFreeTimeMapper;
import com.haier.mapper.HpAreaGoCountMapper;
import com.haier.mapper.HpAreaGoTrackMapper;
import com.haier.mapper.HpAreaMapper;
import com.haier.mapper.HpConstantMapper;
import com.haier.mapper.HpGardenMapper;
import com.haier.service.IHpAreaGoCountService;
import com.haier.utils.EmptyUtils;
import com.haier.utils.GetUtils;
import com.haier.vo.ResponseVo;

/**
 * <p>
 * 通行剩余次数 服务实现类
 * </p>
 *
 * @author luanxuecheng
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class HpAreaGoCountServiceImpl extends ServiceImpl<HpAreaGoCountMapper, HpAreaGoCount>
		implements IHpAreaGoCountService {
	@Autowired
	private HpAreaGoTrackMapper hpAreaGoTrackMapper;
	@Autowired
	private HpAreaMapper hpAreaMapper;
	@Autowired
	private HpAreaFreeTimeMapper hpAreaFreeTimeMapper;
	@Autowired
	private HpConstantMapper hpConstantMapper;
	@Autowired
	private HpAreaGoCountMapper hpAreaGoCountMapper;
	@Autowired
	private HpGardenMapper hpGardenMapper;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ResponseVo myGo(JSONObject params) {
		String jobNumber = params.getString("jobNumber");
		String name = params.getString("name");
		String areaCode = params.getString("areaCode");
		Boolean isTrack = params.getBoolean("isTrack");
		String userLevel = params.getString("userLevel");
		if (EmptyUtils.isEmpty(isTrack)) {
			isTrack = false;
		}
		Integer level = GetUtils.getInt(userLevel);

		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("code", areaCode);
		HpArea hpArea = hpAreaMapper.selectOne(queryWrapper);

		if (EmptyUtils.isEmpty(hpArea)) {
			return ResponseVo.checkError("该地区不存在");
		}
		HpGarden hpGarden = hpGardenMapper.selectById(hpArea.getGardenId());

		JSONObject result = new JSONObject();
		// 设置地区
		result.put("area", hpArea.getName());
		result.put("garden", hpGarden.getName());

		Date date = new Date();// 当前时间

		// 通行有效时间
		HpConstantEntity hpConstantEntity = hpConstantMapper.selectById(HpConstant.CONSTANT_KEY_GO_COUNT_TIME);
		Integer goCountTime = GetUtils.getInt(hpConstantEntity.getHpValue());
		result.put("goCountTime", goCountTime);

		// 工号
		queryWrapper = new QueryWrapper();
		queryWrapper.eq("area_id", hpArea.getId());
		queryWrapper.eq("job_number", jobNumber);
		queryWrapper.eq("is_success", true);
		queryWrapper.orderByDesc("create_time");
		queryWrapper.last("limit 1");
		HpAreaGoTrack track = hpAreaGoTrackMapper.selectOne(queryWrapper);

		// 设置通行时间
		Boolean isGo = false;// 是否在通行时间内
		if (track != null) {
			if (track.getCreateTime().getTime() + goCountTime * 1000 > date.getTime()) {
				result.put("time", track.getCreateTime());
				isGo = true;
			} else {
				result.put("time", date);
			}
		} else {
			result.put("time", date);
		}

		/* 设置剩余次数 */
		String dateStr = GetUtils.getDateString(date, "yyyy-MM-dd");
		String timeStr = GetUtils.getDateString(date, "HH:mm:ss");

		queryWrapper = new QueryWrapper();
		queryWrapper.le("start_time", timeStr);
		queryWrapper.ge("end_time", timeStr);
		queryWrapper.eq("area_id", hpArea.getId());
		// 免费时间
		Integer freeTimeCount = hpAreaFreeTimeMapper.selectCount(queryWrapper);
		queryWrapper = new QueryWrapper();
		queryWrapper.eq("job_number", jobNumber);
		queryWrapper.eq("date_day", dateStr);
		queryWrapper.eq("area_id", hpArea.getId());
		// 剩余次数查询
		HpAreaGoCount hpAreaGoCount = hpAreaGoCountMapper.selectOne(queryWrapper);

		// 设置剩余次数
		Boolean noCount = hpAreaGoCount == null || hpAreaGoCount.getGoCount() <= 0;
		Integer goCount;
		if (noCount) {// 默认0
			goCount = 0;
		} else if (isGo) {// 正在通行显示原来的数量
			goCount = hpAreaGoCount.getGoCount();
		} else if (level >= hpArea.getAreaLevel()) {
			goCount = hpAreaGoCount.getGoCount();
		} else {// 显示剩余的数量
			goCount = hpAreaGoCount.getGoCount() - 1;
		}
		result.put("goCount", goCount);

		Boolean trackSuccess = false;
		/* 设置颜色 */
		if (isGo) {// 如果通行时间内 显示绿色
			result.put("color", HpConstant.MY_GO_1);
		} else if (level >= hpArea.getAreaLevel()) {
			result.put("color", HpConstant.MY_GO_1);
			trackSuccess = true;
		} else if (freeTimeCount > 0) {// 如果是免费时间直接通过
			result.put("color", HpConstant.MY_GO_1);
			trackSuccess = true;
		} else if (noCount) {// 如果没有剩余数量 显示颜色为红
			result.put("color", HpConstant.MY_GO_2);
		} else {// 成功通行
			hpAreaGoCount.setGoCount(goCount);
			hpAreaGoCountMapper.updateById(hpAreaGoCount);
			result.put("color", HpConstant.MY_GO_1);
			trackSuccess = true;
		}

		// 插入轨迹
		if (!isGo && isTrack) {
			track(jobNumber, name, hpArea.getId(), trackSuccess, date);
		}
		return ResponseVo.success(result);
	}

	private void track(String jobNumber, String name, Long areaId, Boolean success, Date date) {
		HpAreaGoTrack track = new HpAreaGoTrack();
		track.setJobNumber(jobNumber);
		track.setName(name);
		track.setIsSuccess(success);
		track.setAreaId(areaId);
		track.setCreateTime(date);
		hpAreaGoTrackMapper.insert(track);
	}

}
